package com.Jhu1.controller;

import com.Jhu1.entity.Material;
import com.Jhu1.service.MaterialService;
import com.Jhu1.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/material")
public class MaterialController {
    @Autowired
    private MaterialService materialService;
    
    @GetMapping("/all")
    public Result<List<Material>> getMaterialList(@RequestParam("name") String name) {
//        System.out.println(name);
        List<Material> materials;
        if (name.equals("undefined")) {
            materials = materialService.getMaterialList("");
        } else {
            materials = materialService.getMaterialList(name);
        }
        
        return Result.success(materials);
    }
    
    @PostMapping("/add")
    public Result<?> addMaterial(
            @RequestParam("materialName") String materialName,
            @RequestParam("materialPrice") double materialPrice) {
        materialService.add(materialName, materialPrice);
        return Result.success();
    }
    
    @DeleteMapping("/delete")
    public Result<?> deleteMaterial(@RequestParam("id") Integer id) {
        materialService.delete(id);
        return Result.success();
    }
    
    @PatchMapping("/update")
    public Result<?> updateMaterial(
            @RequestParam("id") Integer id,
            @RequestParam("materialName") String materialName,
            @RequestParam("materialPrice") double materialPrice) {
        materialService.updateMaterial(id, materialName, materialPrice);
        return Result.success();
    }
}
